package graphs;

import java.awt.Dimension;
import java.awt.Graphics;

import error.GedivaException;

/**
 * class that draws a table for the given data
 * 
 * @author lindsaykubasik
 * 
 */
@SuppressWarnings("serial")
public class Table extends AbstractGraph<String, Double>
{

    /**
     * Constructor for a table
     */
    public Table()
    {
        myData = null;
        myLabels = null;
        myXvals = null;
    }

    /*
     * (non-Javadoc)
     * 
     * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
     */
    public void paintComponent(Graphics pen)
    {
        Dimension canvas = getSize();
        pen.drawString(myLabels.get(0), (canvas.width / 2), 20);
        int maxSize = 0;
        int size = 0;
        int k = 0;
        Double yVal = 0.0;
        int yInc = 0, xInc = 0;
        for (Object x : myXvals)
        {
            if (!(x instanceof String)
                    || !(myData.get(x).get(0) instanceof Double))
                throw new GedivaException("Improper data types.");

            if (myData.get(x).get(0) instanceof Double)

                size = myData.get(x).size();

            if (size > maxSize)
                maxSize = size;
        }
        pen.drawLine(canvas.width - 25, 25, canvas.width - 25,
                canvas.height - 25);
        pen.drawLine(25, canvas.height - 25, canvas.width - 25,
                canvas.height - 25);
        pen.drawLine(25, 25, 25, canvas.height - 25);
        pen.drawLine(25, 25, canvas.width - 25, 25);
        yInc = (canvas.height - 50) / (myXvals.size() + 1);
        xInc = (canvas.width - 50) / (maxSize + 1);
        for (int i = 1; i < myLabels.size(); i++)
        {
            pen.drawString(myLabels.get(i), i * xInc - 25, 50);
        }
        for (Object x : myXvals)
        {
            pen.drawString((String) x, 50, ((k + 1) * yInc) + 50);
            for (int i = 0; i < myData.get(x).size(); i++)
            {
                if (!(x instanceof String)
                        || !(myData.get(x).get(0) instanceof Double))
                    throw new GedivaException("Improper data types.");
                yVal = (Double) myData.get(x).get(i);
                pen.drawLine((i + 1) * xInc + 25, 25, (i + 1) * xInc + 25,
                        canvas.height - 25);
                pen.drawString(yVal + "", ((i + 1) * xInc) + 50,
                        ((k + 1) * yInc) + 50);
            }
            pen.drawLine(25, (k + 1) * yInc + 25, canvas.width - 25, (k + 1)
                    * yInc + 25);
            k++;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see graphs.AbstractGraph#toString()
     */
    public String toString()
    {
        return "Table";
    }
}